package com.facedemo.face;

import com.facedemo.bean.FaceFeature;
import com.facedemo.db.entity.PersonFeature;
import com.facedemo.db.utils.PersonFeatureDaoUtils;
import com.facedemo.utils.LogUtils;

import java.util.ArrayList;
import java.util.List;

//人脸信息管理
public class FaceInfoManger {
    private static volatile FaceInfoManger instance;

    private List<FaceFeature> feature_List = new ArrayList<>();//人脸特征列表
    public Object featureListLock = new Object();
    private boolean isLoadFaceFinish = false;
    private int faceNum = 0;

    public static FaceInfoManger getInstance() {
        if (null == instance) {
            synchronized (FaceInfoManger.class) {
                if (null == instance) {
                    instance = new FaceInfoManger();
                }
            }
        }
        return instance;
    }

    public void loadFaceFromDbInThread(final LoadFaceCallback callback) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                loadFaceFromDb();
                if(callback!=null) {
                    callback.finish();
                }
            }
        }).start();
    }

    /**
     * 从数据库中加载人脸,请在线程中运行
     * */
    public void loadFaceFromDb() {
        isLoadFaceFinish = false;

        synchronized (featureListLock) {
            List<PersonFeature> list = PersonFeatureDaoUtils.loadAll();
            LogUtils.printD("FaceInfoManger 加载人脸:"+list.size());

            feature_List.clear();
            if (list==null || list.size() == 0) {
                isLoadFaceFinish = true;
                return;
            }
            for(PersonFeature item:list) {
                FaceFeature ff = new FaceFeature();
                ff.personId = item.getPersonId();
                ff.faceId = item.getFaceId();
                ff.feature = item.getFeature();
//                ff.feature = DataSwitch.byte2double(item.getFeature());
                feature_List.add(ff);
            }
            faceNum = feature_List.size();
            isLoadFaceFinish = true;
        }
        return;
    }

    /** 是否已经加载人脸完成 */
    public boolean isLoadFaceFinish() {
        return isLoadFaceFinish;
    }

    /**
     * 更新某员工的人脸信息，如果存在则替换，不存在则添加
     * @param info Face_Info人脸信息
     * */
    public void addOrUpdateFace(FaceFeature info) {
        if(info==null) return;

        synchronized (featureListLock){
            //如果存在此员工的人脸信息，则删除
            for(int i=0;i< feature_List.size();i++) {
                if(feature_List.get(i).isSame(info)) {
                    LogUtils.printD("FaceInfoManger 删除人脸信息:empId:"+info.personId+" faceId:"+info.faceId);
                    feature_List.remove(i);
                    break;
                }
            }
            //添加人脸信息
            LogUtils.printD("FaceInfoManger 添加人脸信息:empId:"+info.personId+" faceId:"+info.faceId);
            feature_List.add(info);
            faceNum = feature_List.size();
        }
    }

    /**
     * 删除人脸信息
     * @param empId 人员ID
     * @param faceId 人脸ID
     * */
    public void deleteFace(String empId,Integer faceId) {
        if(empId==null || empId.length()==0 || empId.equals("null")) return;

        synchronized (featureListLock){
            //如果存在此员工的人脸信息，则删除
            for(int i=0;i< feature_List.size();i++) {
                if(feature_List.get(i).isSame(empId,faceId)) {
                    LogUtils.printD("FaceInfoManger 删除人脸信息:empId:"+empId+" faceId:"+faceId);
                    feature_List.remove(i);
                    break;
                }
            }
            faceNum = feature_List.size();
        }
    }

    /**
     * 删除人脸信息
     * @param empId 人员ID
     * */
    public void deleteFace(String empId) {
        if(empId==null || empId.length()==0 || empId.equals("null")) return;

        synchronized (featureListLock){
            //如果存在此员工的人脸信息，则删除
            for(int i=0;i< feature_List.size();i++) {
                if(feature_List.get(i).isSamePerson(empId)) {
                    LogUtils.printD("FaceInfoManger 删除人脸信息:empId:"+empId+" faceId:"+feature_List.get(i).faceId);
                    feature_List.remove(i);
                    i--;
                }
            }
            faceNum = feature_List.size();
        }
    }

    /**
     * 删除全部人脸信息
     * */
    public void deleteAllFace() {
        LogUtils.printD("FaceInfoManger 删除全部人脸信息");
        synchronized (featureListLock){
            feature_List.clear();
            faceNum = feature_List.size();
        }
    }

    public List<FaceFeature> getFeatureList() {
        return feature_List;
    }

    public int getFaceNum() {
        return faceNum;
    }

    public interface LoadFaceCallback {
        void finish();
    }
}
